﻿//------------------------------------------------------------------------------------------------- 
// <copyright file="PluggableTypeCache.cs" company="Christopher Hines">
// Copyright (c) Christopher Hines. All rights reserved.
// </copyright>
// <summary>
//    Provides caching for pluggable types
// </summary>
//-------------------------------------------------------------------------------------------------

namespace Copperfield
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Provides caching for pluggable types
    /// </summary>
    internal static class PluggableTypeCache
    {
        /// <summary>
        /// Gets or sets the pluggable type cache
        /// </summary>
        private static readonly Dictionary<Type, Dictionary<IComparable, Type>> cache = new Dictionary<Type, Dictionary<IComparable, Type>>();

        /// <summary>
        /// Gets the instance dictionary for the specified plugin type
        /// </summary>
        /// <param name="pluginType">The plugin type for which the instance dictionary should be retrieved</param>
        /// <returns>The instance dictionary for the specified plugin type</returns>
        public static Dictionary<IComparable, Type> InstanceDictionaryFor(Type pluginType)
        {
            if (pluginType == null)
            {
                throw new ArgumentNullException("pluginType", "The plugin type can not be null");
            }

            if (!cache.ContainsKey(pluginType))
            {
                cache.Add(pluginType, new Dictionary<IComparable, Type>());
            }

            return cache[pluginType];
        }

        /// <summary>
        /// Clears all instances from the cache
        /// </summary>
        public static void ClearAll()
        {
            cache.Clear();
        }
    }
}
